<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子水平居中</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        .box1 {
            width: 200px;
            height: 100px;
            background-color: pink;
            margin: 100px auto;
        }
        .box2 {
            position: absolute;
            left: 50%;
            margin-left: -100px;
            top: 300px;
            width: 200px;
            height: 100px;
            background-color: lightblue;
        }
        .box3 {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            top: 500px;
            width: 200px;
            height: 100px;
            background-color: lightgreen;
        }
    </style>
</head>
<body>
    <div class="box1">
        设置宽高标准盒子的水平居中, 使用margin: 0 auto;
    </div>
    <div class="box2">
        宽度固定并且设置了绝对定位的水平居中，left:50%; margin-left: -自身宽度;
    </div>
    <div class="box3">
        宽度固定或者不固定并且设置了绝对定位的水平居中，left:50%; transform: translateX(-50%);
    </div>
</body>
</html>